
#include "bits/stdc++.h"

using namespace std;

int main() {
    int n;
    string start;
    cin >> n >> start;

    unordered_map<string, int> d;//次数map
    queue<string> q;//状态队列

    d[start] = 0;
    q.push(start);

    while (!q.empty()) {
        string t = q.front();
        q.pop();
        //找到对应字符串
        for (int i = 0; i < n; ++i) {
            if (t.substr(i, 4) == "2012") {
                cout << d[t];
                return 0;
            }
        }

        for (int i = 0; i < n - 1; ++i) {
            string x = t;
            swap(x[i], x[i + 1]);//状态转移
            if (!d.count(x)) {
                d[x] = d[t] + 1;
                q.push(x);
            }
        }

    }
    cout << -1;

}